package com.servlet;

import com.domain.goods;
import com.service.buy;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class addgood extends HttpServlet {
    protected void doPost(HttpServletRequest request,
                          HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request,
                         HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        goods goods=new goods(
                Integer.valueOf(request.getParameter("id")),
                request.getParameter("name"),
                Integer.valueOf(request.getParameter("amount")),
                Float.valueOf(request.getParameter("price"))
                );
//        ApplicationContext ac=null;
//        String key=
//                WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE;
//        Object attr=getServletContext().getAttribute(key);
//        if (attr!=null)
//        {
//           ac=(WebApplicationContext)attr;
//        }
        ApplicationContext ac=null;
        ac=
                WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
        buy buy= (com.service.buy) ac.getBean("buyImpl");
        buy.addgood(goods);
        response.getWriter().write("添加商品"+goods.getName()+"成功！");
    }
}
